package cn.elwy.rcp.ui.provider;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;

import cn.elwy.rcp.core.model.ui.tree.TreeElement;

/**
 * 标签提供者类，支持树、表格、列表等控件
 * 
 * @author HSQ
 */
public class DataLabelProvider extends LabelProvider implements ITableLabelProvider, ITableColorProvider {

  private DataParser parser;
  private List<Image> needDistroyImages = new LinkedList<Image>();
  private List<Color> needDistoryColors = new LinkedList<Color>();

  public DataLabelProvider() {
  }

  public DataLabelProvider(DataParser parser) {
    this.parser = parser;
  }

  @Override
  public Image getImage(Object element) {
    if (element instanceof TreeElement) {
      TreeElement treeNode = (TreeElement) element;
      if (treeNode.getImage() != null) {
        return null;// ImageUtil.getImage(treeNode.getImage());
      }
    }
    return null;
  }

  @Override
  public String getText(Object element) {
    if (element instanceof TreeElement) {
      return ((TreeElement) element).getName();
    } else {
      return element.toString();
    }
  }

  @Override
  public Image getColumnImage(Object element, int columnIndex) {
    return parser.parseImage(element, columnIndex);
  }

  @Override
  public String getColumnText(Object element, int columnIndex) {
    String[] texts = null;
    if (element instanceof List) {
      texts = (String[]) ((List<?>) element).toArray();
    } else if (element instanceof Object[]) {
      texts = (String[]) element;// item.getTexts();
    } else if (element instanceof TreeElement) {
      TreeElement item = (TreeElement) element;
      texts = item.getTexts();
      if (texts == null) {
        texts = parser.parseTexts(item.getItem());
        item.setTexts(texts);
      }
    }
    if (texts != null && texts.length > columnIndex) {
      Object text = texts[columnIndex];
      return text == null ? "" : text.toString();
    }
    return parser.parseText(element, columnIndex);
  }

  @Override
  public Color getBackground(Object element, int columnIndex) {
    Color color = null;
    if (parser.isSetBackground()) {
      color = parser.getBackgroundColor(element, columnIndex);
    }
    return color;
  }

  @Override
  public Color getForeground(Object element, int columnIndex) {
    Color color = null;
    if (parser.isSetForeground()) {
      color = parser.getForeground(element, columnIndex);
    }
    return color;
  }

  public DataParser getParser() {
    return parser;
  }

  public void setParser(DataParser parser) {
    this.parser = parser;
  }

  public List<Image> getNeedDistroyImages() {
    return this.needDistroyImages;
  }

  public List<Color> getNeedDistroyColors() {
    return this.needDistoryColors;
  }

}
